<?php

class Model_Location {
	
	/**
	 * Get locations by ids
	 * @param array $locationIds
	 * @return array:
	 */
	public static function getLocationByIds($locationIds)
	{
		$cache = Core_Global::getCache();
		
		$locationIds = array_unique($locationIds);
		
		$cacheKeys = array();
		
		foreach($locationIds as $locationId)
		{
			$cacheKeys[] = 'location:' . $locationId;
		}
		
		$result = $cache->getMulti($cacheKeys);
		
		//Miss cache
		if ($result === false || count($result) != count($cacheKeys))
		{
			if ($result === false) $result = array();
			
			$missKeys = array_diff(array_keys($result), array_keys(array_flip($cacheKeys)));
			
			//Get result from database
			$missResult = Business_Location::getLocationByIds(implode($missKeys));
			
			foreach($missResult as $location)
			{
				$cache->write('location:' . $location['ID'], $location, 604800);
			}

			$result = array_merge($result, $missResult);
		}
		
		return $result;
	}
	
	/**
	 * Delete location by ID
	 * @param int $locationId
	 * @return boolean
	 */
	public static function deleteLocationById($locationId)
	{
		$result = Business_Location::deleteLocationById($locationId);
		
		if ($result)
		{
			$cache = Core_Global::getCache();
				
			$cache->delete('location:' .  $locationId);
		}
		
		return true;
	}
	
}